<?php
/**
 * Show the authors (ONLY FOR POST FEEDS)
 *
 * @author University of London Computer Centre
 * @version 1.3.0 - Release Candidate 1
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 */

// globals
global $ArchivePress;

// actions URL
$responder = get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/responder.php&amp;back=authors';

// get info to apply the filters
// STATUS (only ALL)
$status = "all";


// SEARCH FIELD
$search = "";
if(isset($_GET["s"])){
    $search = stripslashes($_GET["s"]);
}

// BLOGS LIST
$parentblog = "";
if(isset($_GET["parentblog"])){
    $parentblog = attribute_escape($_GET["parentblog"]);
    if($parentblog == "ALL"){
        $parentblog = "";
    }
}
$optionBlogs = "";
$blogs = $ArchivePress->getFeeds(false, false);
foreach($blogs as $blog) {
    $optionBlogs .= '<option value="'.$blog["id"].'"'; if($blog["id"] == $parentblog) $optionBlogs .= ' selected="selected"'; $optionBlogs .='>'.$ArchivePress->cleanOutput($blog["name"]).'</option>';
}


// CHECK FOR MESSAGES
$message = "";
// delete
if(array_key_exists("deletelog", $_GET)){
    if((int)$_GET["deletelog"] == 1){
        // SINGLE ACTION
        $message = __("1 Log Deleted", ARCHIVEPRESS);
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Logs Deleted",ARCHIVEPRESS), (int)$_GET["deletelog"]);
    }
}

// logon
if(array_key_exists("logon", $_GET)){
    $message = __("ArchivePress Log System ON", ARCHIVEPRESS);
}

// logoff
if(array_key_exists("logoff", $_GET)){
    $message = __("ArchivePress Log System OFF", ARCHIVEPRESS);
}

// logempty
if(array_key_exists("logempty", $_GET)){
    $message = __("All the logs have been deleted from the database", ARCHIVEPRESS);
}


if($message != ""){
    $message = '<div class="updated fade below-h2" id="message">
                    <p>
                        '.$message.'<br />
                    </p>
                </div>';
}



echo '<div class="wrap">
        <div class="icon32" id="icon-archivepress"><br /></div>
        <h2>'.__("Post Authors", ARCHIVEPRESS).'</h2>

        '.$message.'

        <form method="get" action="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=show_authors', WP_ARCHIVEPRESS_PREFIX . 'show_authors').'" id="posts-filter">
        <input type="hidden" name="page" value="archivepress/pages/responder.php" />
        <input type="hidden" name="'.WP_ARCHIVEPRESS_PREFIX.'action" value="show_authors" />
        <input type="hidden" name="back" value="authors" />
        <input type="hidden" name="_wpnonce" value="'.wp_create_nonce(WP_ARCHIVEPRESS_PREFIX . 'show_authors').'" />';


// total, basic and debug logs
if($status == "all"){
    echo '
        <ul class="subsubsub">
            <li><a class="current" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/authors.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$ArchivePress->getAuthors().')</span></a></li>
        </ul>';
} else {
    echo '
        <ul class="subsubsub">
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/authors.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$ArchivePress->getAuthors().')</span></a></li>
        </ul>';
}

// PAGINATION
$paging = "";
if(isset($_GET['paging'])){
    $paging = attribute_escape($_GET['paging']);
}
$items = count($ArchivePress->getFilteredAuthors($parentblog, $search, ""));

$p = new pagination;
$p->items($items);
$p->limit(15); // Limit entries per page
$p->target(str_replace("&","&amp;",$_SERVER['REQUEST_URI']));
$p->currentPage($paging); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(2); //No. of page away from the current page

if($paging == "") {
    $p->page = 1;
} else {
    $p->page = $paging;
}

$p->prevT = "";
$p->nextT = "";
$p->displaying = __("Displaying", ARCHIVEPRESS);
$p->of = __("of", ARCHIVEPRESS);

// limit for query
$limit = " LIMIT " . ($p->page - 1) * $p->limit  . ", " . $p->limit;

echo'
        <p class="search-box">
            <label for="post-search-input" class="screen-reader-text">'.__("Search Authors", ARCHIVEPRESS).':</label>
            <input type="text" value="'.$ArchivePress->cleanOutput($search).'" name="s" id="post-search-input" />
            <input type="submit" class="button" value="'.__("Search Authors", ARCHIVEPRESS).'" />
        </p>

        <div class="tablenav">

            <div class="alignleft actions">
                <select class="postform" name="parentblog">
                    <option value="ALL"'; if($parentblog == "") echo ' selected="selected"'; echo'>'.__("View all Blogs", ARCHIVEPRESS).'</option>
                    '.$optionBlogs.'
                </select>
                <input type="submit" class="button-secondary" value="Filter" id="post-query-submit" />
            </div>

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>

            <div class="clear"></div>
        </div>

        <div class="clear"></div>

        <table cellspacing="0" class="widefat post fixed">
            <thead>
                <tr>
                    <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_username" scope="col">'.__("Username", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_oriname"  scope="col">'.__("Original Name", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_parentblog"  scope="col">'.__("Blog", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_post"  scope="col">'.__("First Post", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_posts" scope="col">'.__("Posts", ARCHIVEPRESS).'</th>
                </tr>
            </thead>

            <tfoot>
                <tr>
                    <th style="" class="manage-column column-cb check-column" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_username" scope="col">'.__("Username", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_oriname" scope="col">'.__("Original Name", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_parentblog" scope="col">'.__("Blog", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_post" scope="col">'.__("First Post", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_posts" scope="col">'.__("Posts", ARCHIVEPRESS).'</th>
                </tr>
            </tfoot>

            <tbody>';

// get logs
$users = $ArchivePress->getFilteredAuthors($parentblog, $search, "", $limit);


// print feeds
if(count($users) > 0){
    foreach($users as $user){
            // get blog info
            $feed = $ArchivePress->getFeed((int)$user["meta"][WP_ARCHIVEPRESS_PREFIX."feed_id"]);

            // get post info
            $post = get_post($user["meta"][WP_ARCHIVEPRESS_PREFIX."add_post_id"]);

            echo'
                    <tr valign="top" class="alternate iedit" id="author'.$user["id"].'">
                        <th class="check-column" scope="row"><input type="checkbox" value="'.$user["id"].'" name="author[]" /></th>
                        <td class="column-arpr_username"><a title="'.__("Edit User", ARCHIVEPRESS).'" href="user-edit.php?user_id='.$user["id"].'">'.$ArchivePress->cleanOutput($user["username"]).'</a>
                            <div class="row-actions">
                                <span class="edit"><a title="'.__("Edit User", ARCHIVEPRESS).'" href="user-edit.php?user_id='.$user["id"].'">'.__("Edit", ARCHIVEPRESS).'</a> | </span>
                                <span class="trash"><a title="'.__("Delete this author", ARCHIVEPRESS).'" href="'.wp_nonce_url('users.php?action=delete&user='.$user["id"], 'bulk-users').'">'.__("Delete", ARCHIVEPRESS).' </a></span>
                            </div>
                        </td>
                        <td class="column-arpr_oriname">'.$ArchivePress->cleanOutput($user["meta"][WP_ARCHIVEPRESS_PREFIX."ori_username"]).'</td>
                        <td class="column-arpr_parentblog"><a href="?page=archivepress/pages/edit-posts.php&amp;s='.$ArchivePress->cleanOutput($feed["name"]).'" title="'.$ArchivePress->cleanOutput($feed["name"]).'">'.$ArchivePress->cleanOutput($feed["name"]).'</a></td>
                        <td class="column-arpr_post"><a title="'.__("View Post", ARCHIVEPRESS).'" href="'.get_permalink($user["meta"][WP_ARCHIVEPRESS_PREFIX."add_post_id"]).'">'.$ArchivePress->cleanOutput($post->post_title).'</a></td>
                        <td class="column-arpr_posts"><a title="'.__("Posts", ARCHIVEPRESS).'" href="edit.php?author='.$user["id"].'">'.$ArchivePress->cleanOutput($user["posts"]).'</a></td>
                    </tr>';
    }
} else {
    echo '<tr valign="top" class="alternate iedit" id="feed-0"><td colspan="6">'.__("No Author found", ARCHIVEPRESS).'</td></tr>';
}

echo'
            </tbody>
        </table>
        <div class="tablenav">

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>
            <br class="clear" />
        </div>
    </form>
    </div>';

?>